
angular.module('app').controller('signCtrl', ['$scope', '$rootScope', '$http', '$q', 'utils','$state','activity'
    , function ($scope, $rootScope, $http, $q, utils,$state,activity) {

        $scope.signing = false; //是否签到

        $scope.$on('$viewContentLoaded', function(event) {
            setTimeout(function () {
                if($rootScope.isapp) {
                    wv.setHeader('每日签到','1','签到规则','6')
                }
            },300)
        });

        /**
         * 详情页
         * @param goodsCode
         */
        $scope.toDetail=function (good) {
            if(good.goodsCode){
                $state.go('integral_product_details',{id:good.goodsCode,li:good.limitType})
            }
        };


        $scope.findMore=function(){
            $state.go('integral_main')
        };

        //添加今天签到
        $("#integral-signIn").on('click',function () {
            if(!activity.getSoloCode()){
                if($rootScope.isapp){
                    wv.go('2');
                }else {
                    $state.go('register');
                }
            }else if ($scope.signing == false) {
                $scope.toSign();
            }
        });
        /**
         * 去签到
         */
        $scope.toSign = function () {
            $http.get(utils.integralSys, {
                params: {
                    header: utils.header("300307"),
                    body: {
                        userCode: activity.getSoloCode(),
                    }
                }
            }).then(function (res) {
                if (res.header.re_code == "0000") {
                    $scope.signing=true;
                    $scope.signBonus='+'+res.body.signBonus;
                    $('#integral-sign-txt').text('已签到');
                    $scope.cale.SignIn();
                    $scope.getIntegral();
                } else if (res.header.re_code == "0001") {

                }
            })
        };
        //今天是否签到
        $scope.checkSign= function (d1, d2) {
            return (d1.getFullYear() == d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate());
        };
        /**
         * 推荐产品
         */
        $scope.getRecommendGoods = function () {
            $http.get(utils.integralSys, {
                params: {
                    header: utils.header("300121"),
                    body: {
                    }
                }
            }).then(function (res) {
                if (res.header.re_code == "0000") {
                    $scope.goods=res.body.goods;
                }
            })
        };
        $scope.getIntegral=function () {
            if(activity.getSoloCode()){
                /**
                 * 请求签到情况
                 */
                $http.get(utils.integralSys, {
                    params: {
                        header: utils.header("300312"),
                        body: {
                            userCode: activity.getSoloCode(),
                        }
                    }
                }).then(function (res) {
                    if (res.header.re_code == "0000") {
                        $scope.sign = res.body;
                        if (res.body.signTimes) {
                            for (var i = 0; i <res.body.signTimes.length; i++) {
                                if ($scope.checkSign(new Date(), new Date(res.body.signTimes[i]))) {
                                    $scope.signing=true;
                                    $('#integral-sign-txt').text('已签到');
                                }

                            }
                        }
                        $scope.signBonus='+'+res.body.signBonus;
                        $scope.signTimes= res.body.signTimes;
                    }
                });
            }else {
                $('#integral-sign-txt').text('去登陆');
                //添加今天签到
                $scope.sign={
                    availableBonus:'--',
                    days:'--',
                };

            }

        };

        /**
         * 是否售完
         * @param goods
         * @returns {boolean}
         */
        $scope.isSellout=function (remaining) {
            $scope.remaining=remaining;
            if(remaining>0){
                return false;
            }else {
                return true;
            }
        };


        /**
         * 获取积分
         */
        $scope.getIntegral();
        /**
         * 获取推荐商品
         */
        $scope.getRecommendGoods();

    }]);



